package com.gz.servlet;

import com.google.gson.Gson;
import com.gz.entity.User;
import com.gz.service.UserService;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.HashSet;

//原生Ajax请求
@WebServlet(urlPatterns = "/checkUserServlet")
public class CheckUserServlet extends HttpServlet {
    private UserService userService =new UserService();
    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        doPost(request, response);
    }

    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
            String username = request.getParameter("username");
//        System.out.println(uname);
         response.setContentType("text/html;charset=utf-8");
         //到DB查询
        User userByName = userService.getUserByName(username);
        if (userByName != null) {//说明用户存在，返回用户的json格式字符串
            String strUser = new Gson().toJson(userByName);
            response.getWriter().write(strUser);
        }else {
            response.getWriter().write("");
        }
    }
}
